//
//  RImgOlineViewController.m
//  Riji
//
//  Created by  on 2022/8/21.
//

#import "RImgOlineViewController.h"
#import "RPhotoCell.h"

@interface RImgOlineViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray <KImage*>*array;
@property (nonatomic, strong) NSMutableArray <KImage*>*result;
@property (nonatomic, strong) NSMutableArray *selectArray;
@end

@implementation RImgOlineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"online"),bg_sqlValue(@(YES))];
    self.array = [KImage bg_find:nil where:where];
    [CCHttp http].basicURLString = @"";
    [CCHttp get:api_img para:nil header:nil success:^(id  _Nonnull response) {
        NSArray *array = [[response valueForKey:@"res"] valueForKey:@"category"];
        if (array.count > 0) {
            NSArray *arr1 = [KImage mj_objectArrayWithKeyValuesArray:array];
            NSMutableArray *arr2 = [NSMutableArray array];
            for (KImage *model in arr1) {
                model.online = YES;
                [arr2 addObject:model];
                [KImage bg_saveOrUpdateArray:arr2];
            }
            if (self.array.count <= 0) {
                self.array = arr2;
            }
        }
    } fail:^(NSError * _Nonnull error) { }];
    [CCHttp http].basicURLString = baseurl;
}

- (void)initUI{
    self.title = LS(@"选择图片");
    self.result = [NSMutableArray array];
    self.selectArray = [NSMutableArray array];
    [self.backBtn setImage:[UIImage imageNamed:@"叉"] forState:UIControlStateNormal];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, StuBarHight + 5, 40, 40)];
    [btn setImage:[UIImage imageNamed:@"勾"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(actionForRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.collectionView registerNib:[RPhotoCell nib] forCellWithReuseIdentifier:[RPhotoCell ID]];
}
- (void)setArray:(NSArray<KImage *> *)array{
    _array = array;
    [self.selectArray removeAllObjects];
    for (int i = 0; i<array.count; i++) {
        [self.selectArray addObject:@"0"];
    }
    [self.collectionView reloadData];
}
#pragma mark - 勾勾按钮方法
-(void)actionForRightBtn:(UIButton*)sender{
    if (self.result.count <= 0) {
        return [RLHud msg:LS(@"至少选择一张图片")];
    }
    if (self.resultBlock) {
        self.resultBlock(self.result);
    }
    [self backBtnAction];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RPhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RPhotoCell ID] forIndexPath:indexPath];
    KImage *model = self.array[indexPath.row];
    [UIImage imageWithURL:model.url block:^(UIImage * _Nonnull image) {
        model.data = UIImagePNGRepresentation(image);
        cell.img.image = image;
        cell.selectBtn.hidden = NO;
        cell.selectBtn.selected = [self.selectArray[indexPath.row] boolValue];
    }];
    
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    KImage *model = self.array[indexPath.row];
    RPhotoCell *cell = (RPhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
    WS(self)
    if (cell.selectBtn.hidden == NO) {
        cell.selectBtn.selected = !cell.selectBtn.selected;
        [weakself.selectArray replaceObjectAtIndex:indexPath.row withObject:cell.selectBtn.selected ? @"1":@"0"];
        if (cell.selectBtn.selected) {
            [weakself.result addObject:model];
        }else{
            [weakself.result removeObject:model];
        }
        [collectionView reloadData];
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 15 * 4) / 3;
    return  CGSizeMake(w,w);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(15, 15, 15, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

@end
